package com.example.aopdemo.demos.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
@Component
@Order(3)
public class AspectDemo {
    @Pointcut("execution(* com.example.aopdemo.demos.controller.*.*(..))")
    public void pt(){

    }

    // 第一个.*是所有的类都生效,第二个.*是所有的方法都生效
    @Before("pt()")
    public void doBefore(){
        log.info("执行AspectDemo before...");
    }

    @After("pt()")
    public void doAfter(){
        log.info("执行AspectDemo after...");
    }

    @AfterReturning("pt()")
    public void doAfterReturning(){
        log.info("执行AspectDemo afterReturning...");
    }

    @AfterThrowing("execution(* com.example.aopdemo.demos.controller.*.*(..))")
    public void doAfterThrowing(){
        log.info("执行AspectDemo afterThrowing...");
    }

    @Around("execution(* com.example.aopdemo.demos.controller.*.*(..))")
    // Around必须返回结果
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("执行AspectDemo around 前...");
        // 调用目标方法,执行结果
        Object result = joinPoint.proceed();

        log.info("执行AspectDemo around 后...");

        return result;
    }
}
